<template>
  <el-dialog :visible.sync="dialogVisible" title="商品审核" width="30%" @close="closeDialog">
    <el-form :model="ruleForm" ref="ruleFormRef" :rules="rules" label-width="100px">
      <el-form-item label="审核结果" prop="isAudit">
        <el-select v-model="ruleForm.isAudit" placeholder="请选择" style="width: 100%;">
          <el-option label="通过" :value="1"></el-option>
          <el-option label="不通过" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核备注" prop="auditRemarks">
        <el-input v-model="ruleForm.auditRemarks" type="textarea" placeholder="请输入备注原因"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitHandle" v-loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  auditGoodsInfoAutotrophy
} from '@/api/proprietaryGoods';
export default {
  name: 'CheckDialog',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    goodsId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        isAudit: '',
        auditRemarks: ''
      },
      loading: false
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show
      },
      set(val) {
        this.$emit('update:show', val)
      }
    },
    rules: function() {
      if(this.ruleForm.isAudit == '2') {
        return {
          auditRemarks: [
            { required: true, message: '请输入审核备注', trigger: ['blur', 'change'] }
          ],
          isAudit: [
            { required: true, message: '请选择审核结果', trigger: 'change' }
          ]
        }
      } else {
        return {
          isAudit: [
            { required: true, message: '请选择审核结果', trigger: 'change' }
          ]
        }
      }
    }
  },
  methods: {
    submitHandle() {
      this.$refs.ruleFormRef.validate((valid) => {
        if (valid) {
          const loginInfo = localStorage.getItem('logininfo');
          const orgSeq = (loginInfo && JSON.parse(loginInfo).orgSeq) || '';

          this.loading = true;
          auditGoodsInfoAutotrophy({
            ...this.ruleForm,
            upOrgSeq: orgSeq,
            goodsId: this.goodsId
          }).then(res => {
            if(res.code == 1) {
              this.dialogVisible = false;
              this.$emit('change');
              this.$message.success(res.message || '操作成功');
            } else {
              this.$message.error(res.message)
            }
          }).finally(() => {
            this.loading = false
          })
        } else {
          return false;
        }
      })
    },

    closeDialog() {
      this.$refs.ruleFormRef.resetFields();
      this.$emit('close')
    }
  }
}
</script>